package com.varian.redis.aspect;

import com.varian.redis.operation.AbstractVarianOperation;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ben
 * @since 2024/9/29
 */
@Getter
public class VarianCacheContext {

    private final Map<Class<?>, List<AbstractVarianOperation>> operationMap;
    private final Object target;
    private final Method method;
    private final Object[] args;
    @Setter
    private boolean processed;

    public VarianCacheContext(List<AbstractVarianOperation> operations, Object target, Method method, Object[] args) {
        this.operationMap = operations.stream().collect(Collectors.groupingBy(AbstractVarianOperation::getClass));
        this.target = target;
        this.method = method;
        this.args = args;
        this.processed = false;
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> getOperations(Class<T> clazz) {
        return ((List<T>) operationMap.get(clazz));
    }
}
